﻿Public Class TMyDatabasePrivilegesList
    Implements System.IDisposable

    Private _Owner As TMyUser
    Private _List As New List(Of TMyDatabasePrivileges)
    Public ReadOnly Property Host() As String
        Get
            Return _Owner.Host
        End Get
    End Property
    Public ReadOnly Property User() As String
        Get
            Return _Owner.UserName
        End Get
    End Property
    Public Sub New(ByVal AOwner As TMyUser)
        _Owner = AOwner
    End Sub
    Private ReadOnly Property Query() As TMyQuery
        Get
            Return _Owner.Query
        End Get
    End Property

    Default Public ReadOnly Property Items(ByVal ADatabaseName As String) As TMyDatabasePrivileges
        Get
            For Each item As TMyDatabasePrivileges In _List
                If item.DB.Equals(ADatabaseName) Then
                    Return item
                End If
            Next
            Return Nothing
        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property

    Public Sub Refresh()
        _List.Clear()
        Query.Clear()
        Query.SQL.AppendLine("select")
        Query.SQL.AppendLine(" Db")
        Query.SQL.AppendLine(",Select_priv")
        Query.SQL.AppendLine(",Insert_priv")
        Query.SQL.AppendLine(",Update_priv")
        Query.SQL.AppendLine(",Delete_priv")
        Query.SQL.AppendLine(",Create_priv")
        Query.SQL.AppendLine(",Drop_priv")
        Query.SQL.AppendLine(",Grant_priv")
        Query.SQL.AppendLine(",References_priv")
        Query.SQL.AppendLine(",Index_priv")
        Query.SQL.AppendLine(",Alter_priv")
        Query.SQL.AppendLine(",Create_tmp_table_priv")
        Query.SQL.AppendLine(",Lock_tables_priv")
        Query.SQL.AppendLine(",Create_view_priv")
        Query.SQL.AppendLine(",Show_view_priv")
        Query.SQL.AppendLine(",Create_routine_priv")
        Query.SQL.AppendLine(",Alter_routine_priv")
        Query.SQL.AppendLine(",Execute_priv")
        Query.SQL.AppendLine(",Event_priv")
        Query.SQL.AppendLine(",Trigger_priv")
        Query.SQL.AppendLine("from")
        Query.SQL.AppendLine(" mysql.db")
        Query.SQL.AppendLine("where")
        Query.SQL.AppendLine("host=?host")
        Query.SQL.AppendLine("and user=?user")
        Query.SQL.AppendLine("order by")
        Query.SQL.AppendLine(" Db")
        Query.DeclareAndSet("host", _Owner.Host)
        Query.DeclareAndSet("user", _Owner.UserName)
        Query.Execute()
        While Not Query.Eof
            Dim item As New TMyDatabasePrivileges
            item._DB = Query.FieldAsString("Db")
            item._SelectPriv = Query.FieldAsString("Select_priv").Equals("Y")
            item._InsertPriv = Query.FieldAsString("Insert_priv").Equals("Y")
            item._UpdatePriv = Query.FieldAsString("Update_priv").Equals("Y")
            item._DeletePriv = Query.FieldAsString("Delete_priv").Equals("Y")
            item._CreatePriv = Query.FieldAsString("Create_priv").Equals("Y")
            item._DropPriv = Query.FieldAsString("Drop_priv").Equals("Y")
            item._GrantPriv = Query.FieldAsString("Grant_priv").Equals("Y")
            item._ReferencesPriv = Query.FieldAsString("References_priv").Equals("Y")
            item._IndexPriv = Query.FieldAsString("Index_priv").Equals("Y")
            item._AlterPriv = Query.FieldAsString("Alter_priv").Equals("Y")
            item._CreateTmpTablePriv = Query.FieldAsString("Create_tmp_table_priv").Equals("Y")
            item._LockTablesPriv = Query.FieldAsString("Lock_tables_priv").Equals("Y")
            item._CreateViewPriv = Query.FieldAsString("Create_view_priv").Equals("Y")
            item._ShowViewPriv = Query.FieldAsString("Show_view_priv").Equals("Y")
            item._CreateRoutinePriv = Query.FieldAsString("Create_routine_priv").Equals("Y")
            item._AlterRoutinePriv = Query.FieldAsString("Alter_routine_priv").Equals("Y")
            item._ExecutePriv = Query.FieldAsString("Execute_priv").Equals("Y")
            item._EventPriv = Query.FieldAsString("Event_priv").Equals("Y")
            item._TriggerPriv = Query.FieldAsString("Trigger_priv").Equals("Y")
            _List.Add(item)
            Query.MoveNext()
        End While
        Query.Close()
    End Sub

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            _List.Clear()
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
